//
//  PlayViewController.m
//  Jwy-ijkplayerDemo
//
//  Created by XiaoMing on 16/8/11.
//  Copyright © 2016年 JWY. All rights reserved.
//

#import "PlayViewController.h"
#import <IJKMediaFramework/IJKMediaFramework.h>
#import "UIImageView+WebCache.h"
#import "DMHeartFlyView.h"
@interface PlayViewController ()
@property (atomic, strong) NSURL *url;

@property (atomic, retain) id <IJKMediaPlayback> player;

@property (weak, nonatomic) UIView *PlayerView;

@property (nonatomic, strong)UIImageView *backImg;

@end

@implementation PlayViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    [self creatUI];
    [self installMovieNotificationObservers];
    
}
-(void)viewWillAppear:(BOOL)animated{
    if (![self.player isPlaying]) {
        // 准备播放
        [self.player prepareToPlay];
    }
}

-(void)creatUI{
    self.view.backgroundColor = [UIColor blackColor];
    //直播视频
    self.url = [NSURL URLWithString:_playUrl];
    _player = [[IJKFFMoviePlayerController alloc] initWithContentURL:self.url withOptions:nil];
    UIView *playerView = [self.player view];
    UIView *displayView = [[UIView alloc] initWithFrame:self.view.bounds];
    self.PlayerView = displayView;
    self.PlayerView.backgroundColor = [UIColor blackColor];
    [self.view addSubview:self.PlayerView];
    playerView.frame = self.PlayerView.bounds;
    playerView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
    [self.PlayerView insertSubview:playerView atIndex:1];
    [_player setScalingMode:IJKMPMovieScalingModeAspectFill];
    
    [self addMohuView];
    [self addBtn];
}
// 加载图
- (void)addMohuView
{
    self.backImg = [[UIImageView alloc] initWithFrame:self.view.bounds];
    [_backImg sd_setImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"http://img.meelive.cn/%@",_imageUrl]]];
    UIVisualEffect *blurEffect = [UIBlurEffect effectWithStyle:UIBlurEffectStyleLight];
    UIVisualEffectView *visualEffectView = [[UIVisualEffectView alloc] initWithEffect:blurEffect];
    visualEffectView.frame = _backImg.bounds;
    [_backImg addSubview:visualEffectView];
    [self.view addSubview:_backImg];
    
}
// 按钮
- (void)addBtn
{
    // 返回
    UIButton * backBtn = [UIButton buttonWithType:(UIButtonTypeCustom)];
    backBtn.frame = CGRectMake(10, 64 / 2 - 8, 33, 33);
    [backBtn setImage:[UIImage imageNamed:@"返回"] forState:(UIControlStateNormal)];
    [backBtn addTarget:self action:@selector(backHome) forControlEvents:(UIControlEventTouchUpInside)];
    backBtn.layer.shadowColor = [UIColor blackColor].CGColor;
    backBtn.layer.shadowOffset = CGSizeMake(0, 0);
    backBtn.layer.shadowOpacity = 0.5;
    backBtn.layer.shadowRadius = 1;
    [self.view addSubview:backBtn];
    
    // 暂停
    UIButton * playBtn = [UIButton buttonWithType:(UIButtonTypeCustom)];
    playBtn.frame = CGRectMake([UIScreen mainScreen].bounds.size.width - 33-10, 64 / 2 - 8, 33, 33);
    
    [playBtn setImage:[UIImage imageNamed:@"暂停"] forState:(UIControlStateNormal)];
    [playBtn setImage:[UIImage imageNamed:@"开始"] forState:(UIControlStateSelected)];
    
    [playBtn addTarget:self action:@selector(playOrStop:) forControlEvents:(UIControlEventTouchUpInside)];
    playBtn.layer.shadowColor = [UIColor blackColor].CGColor;
    playBtn.layer.shadowOffset = CGSizeMake(0, 0);
    playBtn.layer.shadowOpacity = 0.5;
    playBtn.layer.shadowRadius = 1;
    [self.view addSubview:playBtn];
    
    // 点赞
    UIButton * likeBtn = [UIButton buttonWithType:(UIButtonTypeCustom)];
    likeBtn.frame = CGRectMake(playBtn.frame.origin.x/2, [UIScreen mainScreen].bounds.size.height-33-10, 33, 33);
    [likeBtn setImage:[UIImage imageNamed:@"点赞"] forState:(UIControlStateNormal)];
    [likeBtn addTarget:self action:@selector(dianZan:) forControlEvents:(UIControlEventTouchUpInside)];
    likeBtn.layer.shadowColor = [UIColor blackColor].CGColor;
    likeBtn.layer.shadowOffset = CGSizeMake(0, 0);
    likeBtn.layer.shadowOpacity = 0.5;
    likeBtn.layer.shadowRadius = 1;
    likeBtn.adjustsImageWhenHighlighted = NO;
    [self.view addSubview:likeBtn];
    
    
}

// 返回
- (void)backHome
{
    // 停播
    [self.player shutdown];
    
    [self.navigationController popViewControllerAnimated:true];
    
}

// 暂停开始
- (void)playOrStop:(UIButton *)sender {
    
    sender.selected =! sender.selected;
    if (![self.player isPlaying]) {
        // 播放
        [self.player play];
    }else{
        // 暂停
        [self.player pause];
    }
}

// 点赞
-(void)dianZan:(UIButton *)sender{
    CGFloat sizeWH=50;
    DMHeartFlyView* heart = [[DMHeartFlyView alloc]initWithFrame:CGRectMake(0, 0, sizeWH, sizeWH)];
    [self.view addSubview:heart];
    CGPoint fountainSource = CGPointMake(([UIScreen mainScreen].bounds.size.width-sizeWH-10)/2 + sizeWH/2.0, self.view.bounds.size.height - sizeWH/2.0 - 10);
    heart.center = fountainSource;
    [heart animateInView:self.view];
    
    // button点击动画
    CAKeyframeAnimation *btnAnimation = [CAKeyframeAnimation animationWithKeyPath:@"transform.scale"];
    btnAnimation.values = @[@(1.0),@(0.7),@(0.5),@(0.3),@(0.5),@(0.7),@(1.0), @(1.2), @(1.4), @(1.2), @(1.0)];
    btnAnimation.keyTimes = @[@(0.0),@(0.1),@(0.2),@(0.3),@(0.4),@(0.5),@(0.6),@(0.7),@(0.8),@(0.9),@(1.0)];
    btnAnimation.calculationMode = kCAAnimationLinear;
    btnAnimation.duration = 0.3;
    [sender.layer addAnimation:btnAnimation forKey:@"SHOW"];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
#pragma Install Notifiacation

- (void)installMovieNotificationObservers {
    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(moviePlayBackStateDidChange:)
                                                 name:IJKMPMoviePlayerPlaybackStateDidChangeNotification
                                               object:_player];
}
- (void)moviePlayBackStateDidChange:(NSNotification*)notification {
    
    _backImg.hidden = YES;
    
}
- (void)removeMovieNotificationObservers {
    
    [[NSNotificationCenter defaultCenter] removeObserver:self
                                                    name:IJKMPMoviePlayerPlaybackStateDidChangeNotification
                                                  object:_player];
    
}
-(void)dealloc{
    [self removeMovieNotificationObservers];
}
@end
